<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <script src="../socket.io/socket.io.js"></script>
    <!-- <script src="javascripts/jquery-3.1.1.js"></script> -->
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; font-size: 22px;}
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <div style="right:0;position:absolute;"><a href="./home"><img style="width:40px;height:40px;" src="img/home.png" alt="返回主页" /></a></div>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button onclick="sendMsg(event);">Send</button>
    </form>
    <script>
		var socket = io();
		function sendMsg(e) {
			var msgNode = document.getElementById("m");
			socket.emit('chat message', {msg: msgNode.value, name: '<%= name %>'});
			msgNode.value = "";
			e.preventDefault();
			e.stopPropagation();
			//return false; //感觉还是不能作为.preventDefault();stopPropagation();的合成写法
		}


      socket.on('message', function(obj){
        //es6模板字符串语法
		var msgNode = document.getElementById("messages");
		var liNode = document.createElement("li");
		var textNode = document.createTextNode(`${obj.name}: ${obj.msg}`);
		liNode.appendChild(textNode);
		msgNode.appendChild(liNode);
        //$('#messages').append($('<li>').text(`${obj.name}: ${obj.msg}`));
      });
    </script>
  </body>
</html>